package com.mooc.lib_sofa.ui.sofa

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_common.model.FeedDissEntity
import com.mooc.lib_common.model.FeedLikeEntity
import com.mooc.lib_common.model.FeedShareEntity
import com.mooc.lib_common.model.HomeFeedEntity
import com.mooc.lib_network.entity.NetResult
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class SofaViewModel : ViewModel(),OnRefreshLoadMoreListener{

    //请求参数配置
    var feedId = 0
    var pageCount = 20
    var userId = 0
    var sofaRepo : SofaRepo

    init {
        sofaRepo = SofaRepo()
    }
    companion object{
        const val PICS = "pics"
        const val VIDEO = "video"
        const val TXT = "text"
    }
    private var page = 1//页数
    //由于数据为网络获取到数据源->viewmodel中将获取到的数据源传递fragment中
    //基础版本->创建2livedata 提供给fragment订阅->fragment下适配器可根据数据源进行匹配添加
    //adapter.addData()
    //adapter.setNewInstance()
    //复杂版本->由于1fragment下具有3个Recyclerview->每个列表都具有刷新加载问题
    //数据源为非规范数据源->itemtype类型->1,2两个状态->要显示数据源到3个不同类型的列表当中
    //viewmodel协程中直接规划完成3种类型数据源->列表中直接按照item类型使用
    val picsRefreshBus = LiveDataBus<HomeFeedEntity>()
    val picsLoadBus = LiveDataBus<HomeFeedEntity>()
    val videoRefreshBus = LiveDataBus<HomeFeedEntity>()
    val videoLoadBus = LiveDataBus<HomeFeedEntity>()
    val textRefreshBus = LiveDataBus<HomeFeedEntity>()
    val textLoadBus = LiveDataBus<HomeFeedEntity>()
    val likeBus = LiveDataBus<FeedLikeEntity>()
    val dissBus = LiveDataBus<FeedDissEntity>()
    val shareBus = LiveDataBus<FeedShareEntity>()
    var picsPage = 1
    var videPage = 1
    var textPage = 1

    //下拉刷新方法
    fun sofaRefresh(feedType: String){
        viewModelScope.launch (Dispatchers.IO){
            val resultEntity = sofaRepo.requestRefresh(feedId,feedType,pageCount*page,userId)
            if (resultEntity is NetResult.Success){
                if (feedType.equals(VIDEO)){
                    //itemtype=2
                    videoRefreshBus.postValue(resultEntity.data)
                }else if (feedType.equals(PICS)){
                    //itemtype = 1
                    picsRefreshBus.postValue(resultEntity.data)
                }else{
                    //重新匹配数据源->itemType->文字->3
                    var index = 0
                    while (index < resultEntity.data.data!!.size){
                        resultEntity.data.data!![index].itemType = 3
                        index ++
                    }
                    textRefreshBus.postValue(resultEntity.data)
                }
            }
        }

    }

    //上拉加载方法
    fun sofaLoad(feedType: String){
        viewModelScope.launch(Dispatchers.IO){
            val resultEntity = sofaRepo.requestLoad(feedId,feedType,pageCount*page,userId)
            if (resultEntity is NetResult.Success){
                if (feedType.equals(VIDEO)){
                    //itemtype=2
                    videoLoadBus.postValue(resultEntity.data)
                }else if (feedType.equals(PICS)){
                    //itemtype = 1
                    picsLoadBus.postValue(resultEntity.data)
                }else{
                    //重新匹配数据源->itemType->文字->3
                    var index = 0
                    while (index < resultEntity.data.data!!.size){
                        resultEntity.data.data!![index].itemType = 3
                        index ++
                    }
                    textLoadBus.postValue(resultEntity.data)
                }
            }
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        val smart : SmartRefreshLayout = refreshLayout as SmartRefreshLayout
        smart.finishRefresh()
        //区分要下拉刷新的refreshlayout
        val tag = smart.tag as String
        if (tag.equals("pics")){
            picsPage = 1
            page = picsPage
        }else if (tag.equals("video")){
            videPage = 1
            page = videPage
        }else{
            textPage = 1
            page = textPage
        }
        sofaRefresh(tag)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        val smart : SmartRefreshLayout = refreshLayout as SmartRefreshLayout
        smart.finishLoadMore()
        //区分要下拉刷新的refreshlayout
        val tag = smart.tag as String
        if (tag.equals("pics")){
            picsPage++
            page = picsPage
        }else if (tag.equals("video")){
            videPage++
            page = videPage
        }else{
            textPage++
            page = textPage
        }
        sofaLoad(tag)
    }

    //点赞
    fun like(itemId : Long,itemType : String){
        viewModelScope.launch(Dispatchers.IO){
            val entity = sofaRepo.like(itemId,0)
            if (entity is NetResult.Success){
                val resultEntity : FeedLikeEntity = entity.data
                resultEntity.itemId = itemId
                resultEntity.itemType = itemType
                likeBus.postValue(resultEntity)
            }
        }
    }
    //踩
    fun diss(itemId : Long,itemType : String){
        viewModelScope.launch(Dispatchers.IO){
            val entity = sofaRepo.diss(itemId,0)
            if (entity is NetResult.Success){
                val resultEntity : FeedDissEntity = entity.data
                resultEntity.itemId = itemId
                resultEntity.itemType = itemType
                dissBus.postValue(resultEntity)
            }
        }
    }
    //分享
    fun share(itemId : Long,itemType : String){
        viewModelScope.launch(Dispatchers.IO){
            val entity = sofaRepo.share(itemId)
            if (entity is NetResult.Success){
                val resultEntity : FeedShareEntity = entity.data
                resultEntity.itemId = itemId
                resultEntity.itemType = itemType
                shareBus.postValue(resultEntity)
            }
        }
    }
}